//
//  ComtDetailHeaderView.m
//  JiaoX
//
//  Created by yinqy on 15/3/9.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "ComtDetailHeaderView.h"

@interface ComtDetailHeaderView()
{
    UIImageView *_headImage;
    UILabel *_nameLabel;
    UIButton *_timeBtn;
    UIButton *_replyImageBtn;
    
    UIImageView *_genderView;
    UIImageView *_authorView;
    
    UILabel *_contentLabel;
    UIView *_spliteView2;
}

@end
@implementation ComtDetailHeaderView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //1.添加头像
        _headImage = [[UIImageView alloc]initWithFrame:CGRectMake(JXPublicInset, JXPublicInset, HEADIMAGE_HEIGHT, HEADIMAGE_HEIGHT)];
        [_headImage.layer setCornerRadius:HEADIMAGE_HEIGHT*0.5];
        [_headImage.layer setMasksToBounds:YES];
        [self addSubview:_headImage];
        
        //2 添加回复图标
        _replyImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *replyImage = [UIImage imageNamed:@"ic_comt_reply"];
        [_replyImageBtn setImage:replyImage forState:UIControlStateNormal];
        [_replyImageBtn setImage:[UIImage imageNamed:@"ic_comt_reply_heighlight"] forState:UIControlStateSelected];
        [_replyImageBtn setImage:[UIImage imageNamed:@"ic_comt_reply_heighlight"] forState:UIControlStateHighlighted];
        [self addSubview:_replyImageBtn];
        
        //3.添加名称
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_headImage.frame)+JXPublicInset*0.5, CGRectGetMinY(_headImage.frame)+JXPublicInset/4, JXScreenW-JXPublicInset*2-JXPublicInset-HEADIMAGE_HEIGHT, [UIFont systemFontOfSize:NAMELABEL_FONT_SIZE].lineHeight)];
        [_nameLabel setFont:[UIFont systemFontOfSize:NAMELABEL_FONT_SIZE]];
        [_nameLabel setTextColor:JXColor(51, 51, 51)];
        [self addSubview:_nameLabel];
        
        //4.添加时间
        _timeBtn = [[UIButton alloc]initWithFrame:CGRectMake(_nameLabel.x, CGRectGetMaxY(_headImage.frame)-TIMELABEL_FONT_SIZE-JXPublicInset/4, _nameLabel.width, TIMELABEL_FONT_SIZE)];
        [_timeBtn.titleLabel setFont:[UIFont systemFontOfSize:TIMELABEL_FONT_SIZE]];
        [_timeBtn setTitleColor:JXColor(102, 102, 102) forState:UIControlStateNormal];
        [_timeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        [_timeBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [_timeBtn setImage:[UIImage imageNamed:@"ic_time"] forState:UIControlStateNormal];
        [self addSubview:_timeBtn];
        
        //4.1添加用户性别
        _genderView = [[UIImageView alloc]init];
        [self addSubview:_genderView];
        
        //5.添加内容lebel
        _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(_timeBtn.x, CGRectGetMaxY(_timeBtn.frame), JXScreenW- CGRectGetMinX(_timeBtn.frame)-JXPublicInset, CONTENTLABEL_FONT_SIZE)];
        [_contentLabel setFont:[UIFont systemFontOfSize:CONTENTLABEL_FONT_SIZE]];
        [_contentLabel setTextColor:JXColor(51, 51, 51)];
        [_contentLabel setNumberOfLines:0];
        [self addSubview:_contentLabel];
        
        //7.添加第2第分割线
        _spliteView2 =[[UIView alloc]initWithFrame:CGRectMake(_timeBtn.x, CGRectGetMaxY(_contentLabel.frame)+JXPublicInset*0.5, JXScreenW-CGRectGetMinX(_timeBtn.frame), 0.5)];
        [_spliteView2 setBackgroundColor:JXColor(229, 229, 229)];
        [self addSubview:_spliteView2];
        
    }
    return self;
}

-(void)setComtframe:(CommentViewFrame *)comtframe{
    
    [self setFrame:comtframe.frame];
    [_headImage sd_setImageWithURL:[NSURL URLWithString:comtframe.comtInfo.headImage]];
    [_headImage setFrame:comtframe.headImageFrame];
    
    [_nameLabel setText:comtframe.comtInfo.commentName];
    [_nameLabel setFrame:comtframe.nameLabelFrame];
    
    //设置性别
    //默认为未知性别
    NSString *genderImg = @"ic_gender_unknow";
    //判断是男是女
    if ([[NSString stringWithFormat:@"%@",comtframe.comtInfo.commentGender] isEqualToString:@"1"]) {
        genderImg = @"ic_gender_man";
    }else if([[NSString stringWithFormat:@"%@",comtframe.comtInfo.commentGender] isEqualToString:@"2"]){
        genderImg = @"ic_gender_woman";
    }
    //判断是否是vip
    if (comtframe.comtInfo.commentLevel) {
        genderImg = [NSString stringWithFormat:@"%@_vip",genderImg];
    }
    [_genderView setImage:[UIImage imageNamed:genderImg]];
    [_genderView setSize:_genderView.image.size];
    [_genderView setX:CGRectGetMaxX(comtframe.nameLabelFrame)+5];
    [_genderView setY:_nameLabel.centerY-_genderView.height*0.5];
    
    
    [_timeBtn setTitle:comtframe.comtInfo.commentTime forState:UIControlStateNormal];
    [_timeBtn setFrame:comtframe.timeBtnFrame];
    
    [_replyImageBtn setFrame:comtframe.replyBtnFrame];
    [_replyImageBtn setTag:self.tag];
    [_replyImageBtn addTarget:self action:@selector(replyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [_contentLabel setText:comtframe.comtInfo.commentContent];
    [_contentLabel setFrame:comtframe.contentLabelFrame];
    
    [_spliteView2 setFrame:comtframe.splite2Frame];
    
}


-(void)replyBtnClick:(UIButton *)btn{
    if (_delegate !=nil && [_delegate respondsToSelector:@selector(headerViewReplyBtnClick:)]) {
        [self.delegate headerViewReplyBtnClick:btn];
    }
}

@end
